import Layout from '@/layout/index.vue';
import { createRouter, createWebHistory } from 'vue-router';
import { i18n } from '@/i18n'; // 导入全局 i18n 实例

export const routes = [
    {
        path: '/',
        meta: {
            title: '首页',
            icon: '#iconhome',
            hidden: false,
            hasChild: false,
        },
        component: Layout,
        children: [
            {
                path: '/dashboard',
                component: () => import('@/layout/dashboard/index.vue'),
                name: 'dashboard',
                meta: {
                    title: '首页',
                    icon: '#iconhome',
                    hidden: false,
                }
            }
        ]
    },
    {
        path: '/music-manage',
        name: 'music-manage',
        meta: {
            title: '播放器管理',
            icon: '',
            hidden: false,
            hasChild: false,
        },
        component: Layout,
        children: [
            {
                path: '/music-manage/music-player',
                name: 'music-player',
                component: () => import('@/views/music-manage/MusicPlayer.vue'),
                meta: {
                    title: '播放器管理',
                    icon: '#iconcalendarmanage',
                    hidden: false,
                }
            },
        ]
    },
    {
        path: '/login',
        name: 'login',
        meta: {
            title: 'menu.loginPage',
            icon: '',
            hidden: true,
            hasChild: false,
        },
        component: () => import('@/views/login/Login.vue'),
    },
    {
        path: '/article-manage',
        name: 'article-manage',
        meta: {
            title: '文章管理',
            icon: '#iconingredient',
            hidden: false,
            hasChild: true,
        },
        component: Layout,
        children: [
            {
                path: '/article-manage/category-manage',
                name: 'category-manage',
                component: () => import('@/views/category-manage/CategoryManage.vue'),
                meta: {
                    title: '分类管理',
                    icon: '#icondashboard',
                    hidden: false,
                }
            },
            {
                path: '/article-manage/tag-category',
                name: 'tag-category',
                component: () => import('@/views/ingredient-manage/ingredient-category/IngredientCategory.vue'),
                meta: {
                    title: '标签管理',
                    icon: '#iconingredientcategory',
                    hidden: false,
                }
            },
        ]
    },
    {
        path: '/cookbook-manage',
        name: 'cookbook-manage',
        meta: {
            title: 'menu.recipeManagement',
            icon: '#iconcookbookmanage',
            hidden: false,
            hasChild: true,
        },
        component: Layout,
        children: [
            {
                path: '/cookbook-manage/cookbook-list',
                name: 'cookbook-list',
                component: () => import('@/views/cookbook-manage/cookbook-list/cookbookList.vue'),
                meta: {
                    title: 'menu.recipeList',
                    icon: '#iconcookbooklist',
                    hidden: false,
                }
            },
            {
                path: '/cookbook-manage/cookbook-category',
                name: 'cookbook-category',
                component: () => import('@/views/cookbook-manage/cookbook-category/cookbookCategory.vue'),
                meta: {
                    title: 'menu.RecipeClassification',
                    icon: '#iconcookbookcategory',
                    hidden: false,
                }
            },
            {
                path: '/cookbook-manage/cookbook-detail',
                name: 'cookbook-detail',
                component: () => import('@/views/cookbook-manage/cookbook-detail/cookbookDetail.vue'),
                meta: {
                    title: 'menu.RecipeDetails',
                    icon: '',
                    hidden: true,
                }
            },
        ]
    },
    {
        path: '/',
        name: 'calendar',
        meta: {
            title: 'menu.CalendarManagement',
            icon: '#iconcalendarmanage',
            hidden: false,
            hasChild: false,
        },
        component: Layout,
        children: [
            {
                path: '/calendar/daily',
                name: 'calendar/daily',
                component: () => import('@/views/cookbook/daily/Daily.vue'),
                meta: {
                    title: 'menu.CalendarManagement',
                    icon: '#iconcalendarmanage',
                    hidden: false,
                }
            },
        ]
    },
    {
        path: '/system',
        name: 'system',
        meta: {
            title: '系统管理',
            icon: '#iconsystem',
            hidden: false,
            hasChild: true,
        },
        component: Layout,
        children: [
            {
                path: '/system/user',
                name: 'user',
                component: () => import('@/views/system/user/User.vue'),
                meta: {
                    title: '用户管理',
                    icon: '#iconuser',
                    hidden: false,
                }
            },
            {
                path: '/system/role',
                name: 'role',
                component: () => import('@/views/system/role/Role.vue'),
                meta: {
                    title: '角色管理',
                    icon: '#iconrole',
                    hidden: false,
                }
            },
            {
                path: '/system/auth',
                name: 'auth',
                component: () => import('@/views/system/auth/AuthList.vue'),
                meta: {
                    title: '权限管理',
                    icon: '#iconrole',
                    hidden: false,
                }
            },
        ]
    },
    {
        path: '/user/center',
        name: '/user/center',
        meta: {
            title: '个人中心',
            icon: '#iconcalendarmanage',
            hidden: true,
            hasChild: false,
        },
        component: Layout,
        children: [
            {
                path: '/user/center',
                name: 'user-center',
                component: () => import('@/views/user/UserCenter.vue'),
                meta: {
                    title: '个人中心',
                    icon: '#iconcalendarmanage',
                    hidden: true,
                }
            },
        ]
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes,
})

// // 全局前置守卫：动态设置标题
// router.beforeEach((to) => {
//     const nearestWithTitle = to.matched
//         .slice()
//         .reverse()
//         .find((r) => r.meta?.title)

//     if (nearestWithTitle) {
//         // 使用 i18n.global.t() 进行翻译
//         document.title = i18n.global.t(nearestWithTitle.meta.title);
//     } else {
//         document.title = i18n.global.t('page.shijiCloudStack') // 设置默认标题
//     }
// })

// // 全局前置守卫：动态设置标题
// router.afterEach((to) => {
//     const nearestWithTitle = to.matched
//         .slice()
//         .reverse()
//         .find((r) => r.meta?.title)

//     if (nearestWithTitle) {
//         // 使用 i18n.global.t() 进行翻译
//         document.title = i18n.global.t(nearestWithTitle.meta.title);
//     } else {
//         document.title = i18n.global.t('page.shijiCloudStack') // 设置默认标题
//     }
// })

export default router
